package com.twu.biblioteca;
import  java.io.*;
import  java.util.*;
/**
 * Created with IntelliJ IDEA.
 * User: kush jain
 * Date: 7/4/13
 * Time: 8:59 AM
 * To change this template use File | Settings | File Templates.
 */
public class inventoryManager implements Serializable {
    private ArrayList bookList;
    private int nBooks;
    private ArrayList movieList;
    public  String  addNewBook(Book newBook)
    {
        bookList.add(newBook);
        nBooks++;
        return "Added Book " + newBook.getBookInfoinString();
    }
    public  String  addNewMovie(movie newMovie)
    {
        movieList.add(newMovie);
        return "Added Movie " + newMovie.print();
    }
    public  String  removeBook(int bookid)
    {
        int i;
        for(i=0;i<nBooks;i++)
        {
            Book currentBook = (Book)bookList.get(i);
            if(currentBook.getBookId()==bookid)
            {
                break;
            }
        }
        if(i==nBooks)
            return "Book Not Found";

        Book delBook = (Book)bookList.remove(i);
        nBooks--;
        return "Removed Book " + delBook.getBookInfoinString();
    }
    public  String  addNewBook(String title, String author)
    {
        Book newBook  = new Book(nBooks+1);
        newBook.setTitle(title);
        newBook.setAuthor(author);
        return addNewBook(newBook);
    }
    public  String  addNewMovie(String movieName, int year,String director,int rating)
    {
        movie newMovie = new movie(movieName,year,director,rating);
        return addNewMovie(newMovie);
    }
    public  String  addNewMovie(String movieName, int year,String director)
    {
        return addNewMovie(movieName,year,director,Constants.NOT_RATED);
    }
   inventoryManager()
   {
        movieList= new ArrayList();
        bookList= new ArrayList();
        nBooks =0;
   }
   public Book getBookAtIndex(int i)
   {
       if(i>=0 && i<nBooks)
        return (Book)bookList.get(i);
       return new Book();
   }
   public String  getBooksList()
   {
       String listOfBooks = "";
       for(int i=0;i<nBooks;i++)
       {
           listOfBooks = listOfBooks +  ((Book)bookList.get(i)).getBookInfoinString() + Presenter.newLine;

       }
       return listOfBooks.trim();
   }
    public String  getMoviesList()
    {
        String listOfMovies = "";
        for(int i=0;i<movieList.size();i++)
        {
            listOfMovies = listOfMovies +  ((movie)movieList.get(i)).print() + Presenter.newLine;

        }
        return listOfMovies.trim();
    }
   public inventoryManager searchBook(String title)
   {

       inventoryManager  searchResults = new inventoryManager();
       for(int i=0;i<nBooks;i++)
       {
           if(((Book)bookList.get(i)).inTitle(title))
                searchResults.addNewBook(((Book)bookList.get(i)));
       }
       return searchResults;
   }

   public int getNoOfBooks()
   {
       return nBooks;
   }
    public int getNoOfMovies()
    {
        return movieList.size();
    }
    public Book getBookByBookID(int bookid)
    {
        for(int i=0;i<nBooks;i++)
        {
            if(((Book)bookList.get(i)).getBookId()==bookid)
                return ((Book)bookList.get(i));
        }
        return null;
    }
}
